package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Orders;
import com.itheima.bean.ShoppingCart;
import com.itheima.bean.User;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.sevrice.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R add(@RequestBody Orders orders, HttpSession session){
        //取出session 作用域里面的user 用户
        User user = (User) session.getAttribute("user");
        int row = orderService.add(orders,user);
        if (row>0){
         return    R.success("添加订单成功！");
        }
        return R.error("添加订单失败！");
    }

    /**
     * 订单分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        IPage<Orders> page = orderService.findPage(pageParam);
        return R.success(page);
    }

    /**
     * 修改订单状态
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody Orders orders){
        int row = orderService.updateStatus(orders);
        if (row>0){
            return R.success("修改订单状态成功！");
        }
        return R.error("修改订单状态失败！");
    }

    /**
     * 用户id，查询用户订单
     * @param pageParam
     * @return
     */
    @GetMapping("/userPage")
    public R findUserPage(PageParam pageParam){
        IPage<OrdersDto> userPage = orderService.findUserPage(pageParam);
        return R.success(userPage);
    }

    /**
     * 再来一单
     * @return
     */
    @PostMapping("/again")
    public R again(@RequestBody Map<String,String> map){
        String ids = map.get("id");
        long id = Long.parseLong(ids);
        List<ShoppingCart> shoppingCartList = orderService.again(id);

        return R.success(shoppingCartList);
    }
}
